var studentExam={}
var cacheAnswerTimer
studentExam.myExams=function (){
    $.post('studentExam/myExams',null,function (view){
        $('#mainPageContext').html(view)
    })
}
studentExam.selectMyExams=function (){
    $.post('studentExam/myExams', {filterCondition:  $('#student_exam_select_time').val()},function (view){
        $('#exam_table').replaceWith(view)
    })

}
studentExam.intoExam=function (ExamId){
    $.post('studentExam/intoExam',{examId:ExamId},function (result){
        if (result==1){
            alert('时长考试已结束')
            studentExam.myExams()
            return
        }
        if (result==2){
            alert('考试已经完成')
            studentExam.myExams()
            return
        }
        if (result==3){
            alert('考试尚未开始')
            studentExam.myExams()
            return
        }
        if (result==4){
            alert('当前考试已经提交考卷,不能再次进入考试')
            studentExam.myExams()
            return
        }
        if (result==0){
            $.post('studentExam/examPaga.html',{examId:ExamId},function (view){
                $('#mainPageContext').html(view)
            })
        }
    })
}
var interval
studentExam.timerHandler=function (){
    var time = $('#exam-page-time').text()
    var index = time.indexOf('分');
    if (index==-1){
        //表示为区间考试
        var split = time.split(' ')[1].split(':');
        var hour = split[0];
        if (hour=='00'){
            hour = parseInt('0')
        }else {
            hour = parseInt(hour.indexOf('0')==0?hour.replace('0',''):hour)
        }
        var minus = split[1];
        if (minus=='00'){
            minus = parseInt('0')
        }else {
            minus = parseInt(minus.indexOf('0')==0?minus.replace('0',''):minus)
        }
        var second = split[2];
        if (second=='00'){
            second = parseInt('0')
        }else {
            second = parseInt(second.indexOf('0')==0?second.replace('0',''):second)
        }
        //时分秒都是0-60的数字
        interval = window.setInterval(function (){
            index++
            second = second +1
            if (second==60){
                //分钟加一  秒变0
                second = 0
                minus = minus +1
                if (minus==60){
                    hour = hour +1
                    second = 0
                    minus = 0
                    if (hour==24){
                        //小时变0 分钟变0 秒变0 日加1  但是这里不处理  我们的考试应该不会再晚上考吧
                    }
                }
            }

            var result_hour = hour<10?'0'+hour:hour+''
            var result_minus = minus<10?'0'+minus:minus+''
            var result_second = second<10?'0'+second:second+''
            var result_time = result_hour+':'+result_minus+':'+result_second
            $('#exam-page-time').text(result_time)
            var end_time = $('#exam-page-end_time').val()
            if (new Date().getTime()>=new Date(end_time).getTime()){
                alert('考试结束,已经自动提交考卷')
                var param = studentExam.examAnswerConcat()
                param.end_time = end_time
                studentExam.examAnswerSave(param)
                studentExam.myExams()
                clearInterval(interval)
                clearInterval(cacheAnswerTimer)
            }
        },1000)
        //定时器 需要在 考试时间截止 或者 停止考试  后面在做

    }else {
        // 时长考试
        var first_time = $('#exam-page-start_time').val();
        var first_date = new Date(first_time).getTime()
        interval = setInterval(function (){
            var curr_time = new Date().getTime()
            var result_time = curr_time - first_date
            var result_minute = parseInt(result_time/1000/60%60)
            var result_second = parseInt(result_time/1000%60)
            var result_hour = parseInt(result_time/1000/60/60%60)
            var result_time = result_hour+'时:'+result_minute+'分:'+result_second
            $('#exam-page-time').text(result_time)
            //关闭这个定时器
            var student_start_time = $('#exam-page-start_time').val()
            var minutes = parseInt((new Date().getTime() - new Date(student_start_time).getTime())/1000/60)
            if (minutes==parseInt($('#exam-page-duration').val())){
                alert('考试结束,已经自动提交考卷')
                var param = studentExam.examAnswerConcat()
                var result_time = new Date(new Date(student_start_time).getTime() + parseInt($('#exam-page-duration').val())*60*1000)
                var year = result_time.getFullYear()
                var month = result_time.getMonth()<10?'0'+(result_time.getMonth()+1):result_time.getMonth()+1
                var day = result_time.getDay()<10?'0'+result_time.getDay():result_time.getDay()
                var hour = result_time.getHours()
                var minute = result_time.getMinutes()
                var second = result_time.getSeconds()
                var result = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
                console.log(result)
                param.end_time =result
                studentExam.examAnswerSave(param)
                studentExam.myExams()
                clearInterval(interval)
                clearInterval(cacheAnswerTimer)
            }
        },1000)

    }
}
studentExam.prev=function (self){
    var active_question = $('#exam-page-left-content .question.active');
    var prev_question = active_question.prev();
    if (prev_question.length!=0){
        //表示找到上一个
        //切换考题
        active_question.removeClass('active')
        prev_question.addClass('active')
        //切换右侧考题选项
        var right_active = $('#exam-page-right dl dd.active');
        var prev_active = right_active.prev('dd').length!=0?right_active.prev('dd'):'' || right_active.prev('dt').prev('dd').length!=0?right_active.prev('dt').prev('dd'):''
        if (prev_active == ''){
            alert('已经是第一个考题了')
            return
        }

       //表示切换到上一个时 上一个已经完成了 需要将上一个变成当前这个
       right_active.removeClass('active')
       // right_active.addClass('finish')
       prev_active.addClass('active')
        if (prev_active.hasClass('finish')){
            prev_active.removeClass('finish')
        }
        if (prev_active.hasClass('label')){
            prev_active.removeClass('label')
        }

    }else{
        //已经是第一个考试题了
        alert('已经是第一个考题了')
    }

}
studentExam.next=function (self){
    var active_question = $('#exam-page-left-content .question.active');
    var next_question = active_question.next();
    if (next_question.length!=0){
        //表示找到上一个
        //切换考题
        active_question.removeClass('active')
        next_question.addClass('active')
        //切换考号
        var right_active = $('#exam-page-right dl dd.active');
        var next_active = right_active.next('dd').length!=0?right_active.next('dd'):'' || right_active.next('dt').next('dd').length!=0?right_active.next('dt').next('dd'):''
        if (next_active == ''){
            alert('已经是最后一个考题了')
            return
        }
        right_active.removeClass('active')
        right_active.addClass('finish')
        next_active.addClass('active')
        if (next_active.hasClass('finish')){
            next_active.removeClass('finish')
        }
        if (next_active.hasClass('label')){
            next_active.removeClass('label')
        }
    }else{
        //已经是第一个考试题了
        alert('已经是最后一个考题了')
    }
}
studentExam.label=function (self){
    var active = $('#exam-page-right dl dd.active');
    active.addClass('label')
}
studentExam.question_select=function (self,index){
    var active = $('#exam-page-right dl dd.active');
    active.addClass('finish')
    active.removeClass('active')
    $(self).addClass('active')
    if ($(self).hasClass('label')){
        $(self).removeClass('label')
    }
    //切换考题
    var index = parseInt($(self).text());
    $('#exam-page-left-content .question.active').removeClass('active')
    var dd =  $('#exam-page-left-content .question').eq(index-1)
    dd.addClass('active')
}
var question5_list = []

studentExam.optionInitSet=function (){
    //因为如果多个考试都用这个处理question5的富文本编辑器  可能会把另一个考试的文本编辑器也带过来
    if (question5_list.length != 0){
        question5_list = []
    }
    var questions = $('#exam-page-left-content .question');
    questions.each(function (i,question){
        var title = $(question).children('.question-title').children().text();
        var type = title.substring(title.indexOf('.')+1)
        if (type == '单选题' || type == '判断题'){
           $(question).children('.option-box').children('.row').children('.col-md-2').each(function (j,option){
               $(option).click(function (){
                  $(option).parent().parent().children('.row').children('.col-md-2').each(function (x,tempOption){
                      $(tempOption).removeClass('active')
                  })
                   $(option).addClass('active')
               })
           })
        }else if (type== '多选题'){
            $(question).children('.option-box').children('.row').children('.col-md-2').each(function (j,option){
                $(option).click(function (){
                    $(option).hasClass('active')?$(option).removeClass('active'):$(option).addClass('active')
                })
            })

        }else if (type=='综合题'){
            var div = $(question).children('.option-box').children('.row').children('.col-md-2').children('.question5-text')[0];
            var text = $(div).prev('.question5-text-input').val()
            var E = window.wangEditor
            var edit = new E(div);
            edit.config.width = 200
            edit.config.height = 200
            edit.create()
            edit.txt.append(text)

            question5_list.push(edit)
        }
    })
}

//主要是为了中途断线 重连 然后数据还在的处理  我们每隔一分钟缓存你的数据
studentExam.cacheAnswerTimer = function (){
    cacheAnswerTimer = setInterval(function (){
        var param = studentExam.examAnswerConcat()
        studentExam.examAnswerSave(param)
    },1000*60)
}
var commonSeparator = '}-|-{'
var commonNoAnswer = 'no'
var commonHasAnswer = 'yes'
var questions5_symbol = 0 //表示获取综合题的顺序
studentExam.examAnswerConcat=function (){
    var param = {}
    param.question1_answer = ''
    param.question2_answer = ''
    param.question3_answer = ''
    param.question4_answer = ''
    param.question5_answer = ''
    param.examId = $('#exam-page-examId').val()
    param.studentId = $('#exam-page-examStudentId').val()
    $('#exam-page-left-content .question').each(function (i,question){
        var title = $(question).children('.question-title').children().text();
        var type = title.substring(title.indexOf('.')+1)

        if (type == '单选题') {
            var symbol = false//用来表示当前题是否有选择答案
            $(question).children('.option-box').children('.row').children('.question1').each(function (i, option) {
                var statues = $(option).hasClass('active');
                if (statues) {
                    //说明当前单选题选中的是当前答案
                    symbol = true
                    var answer = $(option).text();
                    param.question1_answer = param.question1_answer + answer + commonSeparator
                    //不知道如何break jquery的循环
                }

            })
            if (!symbol) {
                //代表当前单选题 没有选择答案
                param.question1_answer = param.question1_answer + commonNoAnswer + commonSeparator
            }
        }else if (type == '多选题') {
            var symbol = false//用来表示当前题是否有选择答案
            var question2_answer = ''
            $(question).children('.option-box').children('.row').children('.question2').each(function (i, option) {
                var statues = $(option).hasClass('active');
                if (statues) {
                    //说明当前单选题选中的是当前答案
                    if (!symbol) {
                        symbol = true
                    }
                    var answer = $(option).text();
                    question2_answer = question2_answer + answer + '-'
                    //不知道如何break jquery的循环
                }else {
                    question2_answer = question2_answer + ' ' + '-'
                }
            })
            var question2_answer_length = $(question).children('.option-box').children('.row').children('.question2').length
            if (!symbol) {
                //代表当前单选题 没有选择答案
                param.question2_answer = param.question2_answer + commonNoAnswer+'-'+ question2_answer_length + commonSeparator
            } else {
                param.question2_answer = param.question2_answer + question2_answer + commonSeparator
            }
        }else if (type == '判断题') {
            var symbol = false//用来表示当前题是否有选择答案
            $(question).children('.option-box').children('.row').children('.question3').each(function (i, option) {
                var statues = $(option).hasClass('active');
                if (statues) {
                    //说明当前单选题选中的是当前答案
                    symbol = true
                    var answer = $(option).text();
                    param.question3_answer = param.question3_answer + answer + commonSeparator
                    //不知道如何break jquery的循环
                }
            })
            if (!symbol) {
                //代表当前单选题 没有选择答案
                param.question3_answer = param.question3_answer + commonNoAnswer + commonSeparator
            }
        }else if (type == '填空题'){
            //填空题有几个问题  一个填空题 假如有两个空 一个填了 另一个没有填写 那这道题我们按照 yes-no}-|-{.......这样处理
            var currAnswer = ''
            $(question).children('.option-box').children('.row').children('.form-control').each(function (i,input){
                var text = $(input).val()
                if (text == '' || text == undefined){
                    currAnswer = currAnswer + commonNoAnswer +'-'
                }else {
                    currAnswer = currAnswer + text +'-'
                }
            })
            param.question4_answer = param.question4_answer + currAnswer +commonSeparator
        }else if (type == '综合题'){
            //因为综合题的答案都是存放在富文本编辑器中的

                var editor = question5_list[questions5_symbol];
                var text = editor.txt.text();
                if (text == '' || text == undefined){
                    param.question5_answer = param.question5_answer + commonNoAnswer +commonSeparator
                }else {
                    param.question5_answer = param.question5_answer + text +commonSeparator
                }
                questions5_symbol++
            if (i == $('#exam-page-left-content .question').length -1){
                //表示当前这里已经是考题的最后一个处理  如果下次再处理 综合题应该从按照顺序一个一个处理
                questions5_symbol = 0
            }
        }
    })
    return param
}
studentExam.examAnswerSave=function (param){
    $.post('studentExam/examAnswerSave',param,function (){

    })
}
studentExam.commitExam = function (){
    var param = studentExam.examAnswerConcat()
    var result_time = new Date()
    var year = result_time.getFullYear()
    var month = result_time.getMonth()<10?'0'+(result_time.getMonth()+1):result_time.getMonth()+1
    var day = result_time.getDate()<10?'0'+result_time.getDate():result_time.getDate()
    var hour = result_time.getHours()
    var minute = result_time.getMinutes()
    var second = result_time.getSeconds()
    var result = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
    param.end_time = result
    studentExam.examAnswerSave(param)
    alert('交卷成功，即将退出考试')
    studentExam.myExams()
    clearInterval(interval)
    clearInterval(cacheAnswerTimer)
}

//成绩查询
studentExam.studentExamResultPage = function (){
    $.post('studentExam/studentExamResultList.html',null,function (view){
        $('#mainPageContext').html(view)
    })
}
studentExam.selectYearExam = function (){

    $.post('studentExam/selectExamByYear',{year:$('#student_exam_select_time').val()},function (view){
        $('#mainPageContext').html(view)
    })
}